package xing.application.service.task;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import xing.application.service.page.dto.ContentMoveVo;
import xing.domain.page.QPageModuleItem;

/********************************
 * 文 件 名 :MoveContentTask.java
 * 创 建 人：陈天龙
 * 日    期：2017/8/10
 * 描   述：模块内容移动异步任务
 * 版 本 号：v1.0.0
 ********************************/
@Component
public class MoveContentTask{

    private ContentMoveVo contentMoveVo;

    private JPAQueryFactory queryFactory;

    public ContentMoveVo getContentMoveVo() {
        return contentMoveVo;
    }

    public void setContentMoveVo(ContentMoveVo contentMoveVo) {
        this.contentMoveVo = contentMoveVo;
    }

    public JPAQueryFactory getQueryFactory() {
        return queryFactory;
    }

    public void setQueryFactory(JPAQueryFactory queryFactory) {
        this.queryFactory = queryFactory;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Async
    @Transactional(rollbackFor = Exception.class)
    public void move() throws Exception{
        logger.debug("准备开始移动");
         /*
        上移和下移,都是把互换位置的两个内容的sort互换
         */
        QPageModuleItem source = QPageModuleItem.pageModuleItem;
        QPageModuleItem target = QPageModuleItem.pageModuleItem;
        //更新当前操作的内容
        try {
            queryFactory.update(source).where(source.id.longValue().eq(contentMoveVo.getSourceId())).set(source.sort,contentMoveVo.getTargetSort()).execute();
            queryFactory.update(target).where(target.id.longValue().eq(contentMoveVo.getTargetId())).set(target.sort,contentMoveVo.getSourceSort()).execute();
        } catch (Exception e) {
            logger.error("移动失败",e);
            throw e;
        }
//        return new AsyncResult<>("移动成功!");
    }
}
